/*var mongoose=require('mongoose');
var config=require('../config');
var con=config.config;
var mongodb = require('mongodb');*/
var mongoose=require('./mongoConn');
var Schema=mongoose.Schema;
var _ = require("underscore");
/*
var db=mongoose.createConnection(config.db_url);
db.on('error',function(error){
    db.close();
    console.log(error);
});

db.on('close', function () {
    db.open(config.db_url);
    console.log("重新连接mongo");
});
*/

var doorSchema = new Schema({
    index: Number,
    empty: Boolean,
    password: String,
    opentime: Number,
    owner: String, //user or wechat's openid
    prize_id:String, //商品ID
    discount_price:String, //折扣价格
    sex:Number//对应放入奖品人的性别
});
var boxSchema = new Schema({
    box_id: String, // the box id
    code_layout:String, //贴码位置，0:左,1:右
    doors_num:Number, //门的数量，只能为偶数
    box_layout:String, //箱子布局
    doors: [doorSchema],
    happy_times:String,//高中奖率时间段
    probability:Number,//中奖率
    probability1:Number,//中奖率,和高中奖率时段无关，如果设置则不使用箱子统一中奖率
    limit:Boolean//是否开箱后更改箱子状态 true需要 false不需要
});
//var Box = db.model("Box", boxSchema);
var Box = mongoose.model("Box", boxSchema);

Box.prototype.findBoxById=function (boxid,callback){
    Box.find({box_id:boxid}, function(err, datas){
        return callback(datas);
    });
}

Box.prototype.findBoxesById=function (opt,callback){
    Box.find(opt, function(err, datas){
        return callback(datas);
    });
}

Box.prototype.findByBoxId = function(boxid, callback) {
    Box.findOne({
        box_id: boxid
    }, function(err, box) {
        var data = [];
        _.each(box.doors, function(door, index, list) {
            data.push({
                index: door.index,
                empty: door.empty,
                password: door.password,
                opentime: door.opentime,
                owner: door.owner, //user or wechat's openid
                prize_id:door.prize_id, //商品ID
                discount_price:door.discount_price //折扣价格
            });
        });
        callback({boxlimit:box.limit,data:data});
    });
}

Box.prototype.updateByBoxidAndIndex = function(boxid, index,isempty, callback) {
    Box.update({
        "box_id": boxid,
        "doors.index": index
    }, {
        "doors.$.empty": isempty,
        "doors.$.password": "",
        "doors.$.opentime": "",
        "doors.$.owner": ""
    }, function(err) {
        if (err) {
        }
        callback(boxid);
    });
}
Box.prototype.findAllBox=function(callback){
    Box.find({},function(err,datas){
        return callback(err,datas);
    })
}
Box.prototype.updateReBinding = function(boxid, index, callback) {
    Box.update({
        "box_id": boxid,
        "doors.index": index
    }, {
        "doors.$.prize_id": null,
        "doors.$.discount_price": null
    }, function(err) {
        if (err) {
        }
        callback(boxid);
    });
}
Box.prototype.updateBinding = function(boxid, index, prize_id,discount_price,callback) {
    Box.update({
        "box_id": boxid,
        "doors.index": index
    }, {
        "doors.$.prize_id": prize_id,
        "doors.$.discount_price": discount_price
    }, function(err) {
        if (err) {
        }
        callback(boxid);
    });
}

Box.prototype.updateDoorById=function(boxid,doors,callback){
    Box.update({box_id:boxid},{"doors":doors},function(err){
        return callback(err);
    });
}

Box.prototype.updateColumn=function(condition,value,callback){
    Box.update(condition,value,function(err){
        return callback(err);
    });
}

Box.prototype.updateCodeById=function(boxid,code_layout,callback){
    Box.update({box_id:boxid},{"code_layout":code_layout},function(err){
        return callback(err);
    });
}

Box.prototype.updateDoorNumById=function(boxid,doors,door_num,box_layout,callback){
    Box.update({box_id:boxid},{doors:doors,doors_num:door_num,box_layout:box_layout},function(err){
        return callback(err);
    });
}

Box.prototype.updateAllById=function(boxid,doors,door_num,box_layout,code_layout,callback){
    Box.update({box_id:boxid},{doors:doors,doors_num:door_num,box_layout:box_layout,code_layout:code_layout},function(err){
        return callback(err);
    });
}

Box.prototype.removeById=function(boxid,callback){
    Box.remove({box_id:boxid},function(err){
        return callback(err);
    });
}

Box.prototype.findForPage=function(obj,callback){
    var q=obj.search||{};                                       //查询条件
    var col=obj.columns||{};                                    //返回字段
    var query=Box.find(q,col).skip(obj.page.skipFrom).limit(obj.page.limit);

    query.exec(function(err, datas) {
        if (err) {
            callback(err, null, null);
        } else {
            Box.count(q, function(err, count) {
                if (err) {
                    callback(err, null, null);
                } else {
                    var pageCount = Math.ceil(count / obj.page.limit);
                    callback(null,count,pageCount, datas);
                }
            });
        }
    });
}

module.exports = Box;